<?php
/**
 * Joomla! 1.5 component Bbits Simple directory
 *
 * @version $Id: view.html.php 2011-07-08 03:53:16 svn $
 * @author Miguel Puig
 * @URL http://miguelpuig.com
 * @package Joomla
 * @subpackage Bbits Simple directory
 * @license GNU/GPL
 *
 * Simple Directory by bittingbits.com
 *
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class BbitsdModelCountry extends JModel {
    var $_data;
    var $_query;
    
    function __construct()
    {
        parent::__construct();
    }
    
    function _buildQuery()
    {
        $this->_query = 'SELECT * FROM #__bbitsd_items WHERE published=1';
        return $this->_query;
    }

     function array_sort($array, $on, $order='SORT_ASC')
    {
      $new_array = array();
      $sortable_array = array();
 
      if (count($array) > 0) {
          foreach ($array as $k => $v) {
              if (is_array($v)) {
                  foreach ($v as $k2 => $v2) {
                      if ($k2 == $on) {
                          $sortable_array[$k] = $v2;
                      }
                  }
              } else {
                  $sortable_array[$k] = $v;
              }
          }
 
          switch($order)
          {
              case 'SORT_ASC':   
         
                  asort($sortable_array);
              break;
              case 'SORT_DESC':
               
                  arsort($sortable_array);
              break;
          }
 
          foreach($sortable_array as $k => $v) {
              $new_array[] = $array[$k];
          }
      }
      return $new_array;
    } 
    
    
    function getData()
    {
        $this->_buildQuery();
        
        if (empty($this->_data)) {
            $this->_db->setQuery($this->_query);
            $this->_data = $this->_getList($this->_query);
        }
        
        return $this->_data;
    }
    function getCountryByID()
    {
    			$id = JRequest::getVar('id');
    	$query = 'SELECT * FROM #__bbitsd_countries WHERE state=1 AND id='.$id;
    	$this->_db->setQuery($query);
    	$country = $this->_getList($query);
    	return $country[0];
    }

    function getAllData()
    {
     error_reporting(E_ALL);
     ini_set("display_errors", 1); 
     
		$countryId = JRequest::getVar('id');

    	$query = 'SELECT type FROM #__bbitsd_items WHERE published=1 AND country='.$countryId.' GROUP BY type';
    	$this->_db->setQuery($query);
    	$types = $this->_getList($query);
    	
    	$i = 0;
    	foreach ($types as $type):
    	
			$query = 'SELECT id,ordering FROM #__bbitsd_types WHERE id='.$type->type;
			$this->_db->setQuery($query);
			$types_ordered[$i] = $this->_db->loadAssoc();
		
		$i++;
    	endforeach;
	$array_new = $this->array_sort($types_ordered, 'ordering');



    	$c_n = 0;
		foreach ($array_new as $t):
			
			$query = 'SELECT title FROM #__bbitsd_types WHERE id='.$t['id'];
	    	$this->_db->setQuery($query);
	    	$type = $this->_getList($query);
			$types[$c_n]->title = $type[0]->title;
			$query = 'SELECT * FROM #__bbitsd_items WHERE published=1 AND country = '.$countryId.' AND type='.$t['id'].' ORDER BY ordering';
	    	$this->_db->setQuery($query);
	    	$types[$c_n]->countries = $this->_getList($query);
			
		$c_n ++;	
		endforeach;
		
		return $types;

    }
    
    function getModel()
    {
        return $this;
    }
    

}
?>
